package com.liustudy.demo;

import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.StringUtils;
import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.GatewayException;
import org.hyperledger.fabric.gateway.Network;
import org.hyperledger.fabric.sdk.Peer;
import org.springframework.web.bind.annotation.*;

import java.util.EnumSet;
import java.util.Map;

/**
 * @author liuyu
 * @desc:
 * @date 2024/3/28
 */

@RestController
@RequestMapping("/cat")
@Slf4j
@AllArgsConstructor
public class CatContractController {

    final Gateway gateway;


    @GetMapping("/{key}")
    public Map<String, Object> queryCatByKey(@PathVariable String key) throws GatewayException {
        log.info("测试，调用queryCatByKey======》 {}", key);

        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();
        byte[] cat = contract.evaluateTransaction("queryCat", key);

        result.put("payload", StringUtils.newStringUtf8(cat));
        result.put("status", "ok");

        return result;
    }

    @PutMapping("/")
    public Map<String, Object> createCat(@RequestBody CatDto cat) throws Exception {
        log.info("测试，调用createCat======》 {}", cat);
        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();
        byte[] bytes = contract.submitTransaction("createCat", cat.getKey(), cat.getName(), String.valueOf(cat.getAge()), cat.getColor(), cat.getBreed());

        result.put("payload", StringUtils.newStringUtf8(bytes));
        result.put("status", "ok");
        return result;
    }

    @PostMapping("/")
    public Map<String, Object> updateCat(@RequestBody CatDto cat) throws Exception {

        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();
        byte[] bytes = contract.submitTransaction("updateCat", cat.getKey(), cat.getName(), String.valueOf(cat.getAge()), cat.getColor(), cat.getBreed());

        result.put("payload", StringUtils.newStringUtf8(bytes));
        result.put("status", "ok");

        return result;
    }

    @DeleteMapping("/{key}")
    public Map<String, Object> deleteCatByKey(@PathVariable String key) throws Exception {

        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();

//        byte[] cat = contract.submitTransaction("deleteCat", key);
        // 改为所有背书节点都要背书，才算提交成功
        byte[] cat = contract.createTransaction("deleteCat")
                        .setEndorsingPeers(getNetwork().getChannel().getPeers(EnumSet.of(Peer.PeerRole.ENDORSING_PEER)))
                                .submit(key);

        result.put("payload", StringUtils.newStringUtf8(cat));
        result.put("status", "ok");

        return result;
    }


    private Network getNetwork(){
        // 获取通道
        return gateway.getNetwork("mychannel");
    }

    private Contract getContract() {
        return getNetwork().getContract("hyperledger-fabric-contract-java-demo");
    }
}

